package com.shop2cn.iapi.productcategory.testcase;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.shop2cn.iapi.productcategory.parameter.BrandDto;
import com.shop2cn.iapi.productcategory.service.GetAllBrandCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.mongo.BuyerProductShop2cnWapper;
import com.ymttest.utils.DiffUtil;
import com.ymttest.utils.EnvSetup;
/**
*FreeMaker Template自动生成代码
*/
import com.ymttest.utils.Logger2Debug;
import com.ymttest.utils.TestDataManager;

@TestSuite
public class Ts_GetAllBrand {
	private static GetAllBrandCall GetAllBrandCall;
	static Logger2Debug Logger = new Logger2Debug();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取所有的品牌信息");
	}

	@Before
	public void caseUp() {
		// 旧站点
//		GetAllBrandCall = new GetAllBrandCall(EnvSetup.getSystemUrl("productcategory_api"));
		//新站点
		GetAllBrandCall = new GetAllBrandCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(GetAllBrandCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetAllBrand_001() {
		Logger.start(true, "获取所有的品牌信息");
		try {
			BuyerProductShop2cnWapper pw= new BuyerProductShop2cnWapper();
			List<Integer> brandIdList = new ArrayList<Integer>();
			GetAllBrandCall.callService();
			Logger.verifyEquals("200", GetAllBrandCall.getString("Code"), "验证Code");
			JsonArray data = GetAllBrandCall.getData();
			for (JsonElement jsonElement : data) {
				BrandDto branddto=null;
				Map info=null;
				try{
					branddto = (BrandDto) FormatUtil.GSONToObject(jsonElement.getAsJsonObject().toString(), BrandDto.class);
					 info = pw.getProductBrandInfoById(branddto.getBrandId()).get(0);
				}catch (Exception e) {
					//mongo close
					pw=new BuyerProductShop2cnWapper();
					Thread.sleep(1000);
					info = pw.getProductBrandInfoById(branddto.getBrandId()).get(0);
				}
				Logger.verifyEquals(info.get("Alias"), branddto.getAlias(), "验证Alias");
				Logger.verifyEquals(info.get("BrandBannerUrl"), branddto.getBrandBannerUrl(), "验证BrandBannerUrl");
				Logger.verifyEquals(info.get("BrandBizSector"), branddto.getBrandBizSector(), "验证BrandBizSector");
				Logger.verifyEquals(info.get("BrandDescription"), branddto.getBrandDescription(), "验证BrandDescription");
				Logger.verifyEquals(info.get("BrandDescriptionEn"), branddto.getBrandDescriptionEn(), "验证BrandDescriptionEn");
				Logger.verifyEquals(info.get("BrandEnName"), branddto.getBrandEnName(), "验证BrandEnName");
				Logger.verifyEquals(info.get("BrandLogoUrl"), branddto.getBrandLogoUrl(), "验证BrandLogoUrl");
				Logger.verifyEquals(info.get("BrandName"), branddto.getBrandName(), "验证BrandName");
				if(info.get("BrandEnName")!=null&&info.get("BrandEnName").toString().trim().length()>1&&info.get("BrandEnName").toString().matches("^[a-zA-Z]{1}")){
					boolean eq = Logger.verifyEquals(info.get("BrandEnName").toString().trim().substring(0,1).toUpperCase(), branddto.getKeyWord(),"验证KeyWord");
					if(!eq){
						TestDataManager.appendFileToLine("neq.txt", info.get("BrandEnName").toString());
					}
				}
				 //keyword?BrandEnName首字母大写？有些不是,待确认
//				Logger.verifyEquals(0, branddto.getLevel(), "验证Level");
				Logger.verifyEquals(info.get("ProductCount"), branddto.getProductCount(), "验证ProductCount");
				if (info.get("CountryId") != null) {
					Logger.verifyEquals(info.get("CountryId"), branddto.getCountryId(), "验证CountryId");
				} else {
					Logger.debug(branddto.getBrandName() + "not found CountryId");
				}
			}
//			GetAllBrandCall oldGetAllBrandCall = new GetAllBrandCall(EnvSetup.getSystemUrl("productcategory_api"));
//			oldGetAllBrandCall.callService();
//			DiffUtil diff=new DiffUtil();
//			String difffile="diff.txt";
//			diff.setLogFile(difffile);
//			List<String> list=new ArrayList<>();
//			list.add("Data.BrandList.Level");
//			diff.setIgnoreList(list);
//			diff.compareJsonString(GetAllBrandCall.getReturnData(),oldGetAllBrandCall.getReturnData());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}